import { axios } from "@/services/axios";
import {useEffect, useMemo, useState} from "react";

const Customer = {
  get: (code) => axios.get(`api/customer/${code}`),
  query: (bytstatus) => axios.get(`api/customers/list/${bytstatus}`),
  getcustomer: data => axios.get(`api/customers/${data.id}`),
  getcustomerlist: params => axios.post(`api/customers/list`, params),
  createcustomer: data => axios.post("api/customers", data),
  getcustomerid: data => axios.get("api/customers", data),
  savecustomer: data => axios.post(`api/customers/${data.id}`, data),
  deletecustomer: data => axios.post(`api/customers/delete/${data.id}`),
  querydepartment: data => axios.post(`api/department/list`, data),
  queryyusuan: data => axios.post(`api/yusuancode/list`, data),
  querycustomeruser: data => axios.post(`api/customeruser/list`, data),
  
};
export function useOrgunit(orgunitcode) {
  const [optionsLoaded, setOptionsLoaded] = useState(false);
  const [options, setOptions] = useState([]);

  useEffect(() => {
    let cancelDataSourceLoading = false;
    let params={
      code: "",
      name: "",
      userid: 0,
    }
    Customer.querydepartment(params).then(data => {
      if (!cancelDataSourceLoading) {
        setOptionsLoaded(true);
        setOptions(data);
    }});

    return () => {
      cancelDataSourceLoading = true;
    };
  }, [orgunitcode]);

  return useMemo(() => ({ optionsLoaded, options }), [optionsLoaded, options]);
}
export function UseQueryCusomerUser(userid) {
  const [employeeloaded, setemployeeloaded] = useState(false);
  const [employee, setemployee] = useState([]);

  useEffect(() => {
    let cancelDataSourceLoading = false;
    Customer.querycustomeruser({ code: "", name: "", userid: userid, }).then(data => {
      if (!cancelDataSourceLoading) {
        setemployeeloaded(true);
        setemployee(data);
    }});

    return () => {
      cancelDataSourceLoading = true;
    };
  }, [userid]);

  return useMemo(() => ({ employeeloaded, employee }), [employeeloaded, employee]);
}
export default Customer;


